import {observable,action} from "mobx";
import {getSessionStoreage,setSessionStoreage} from '../../util/storeage'
// import axios from 'axios'
import axios from '../../util/http'
import * as url from '../../util/api'
class LoginStore {
    @observable user=getSessionStoreage('user') || {isLogin:false,premissicons:''}

    @action
    loginIn=(obj)=>{
        let {tel,password}=obj
        return axios({
            url:url.userlogin,
            method:'post',
            data:{
                tel,password
            }
        }).then(res=>{
            console.log(res)
            // 这里应该设置token
            //设置个人信息
            if(res.data.code===200){
                let user={
                    isLogin:true,
                    premissicons:res.data.data.username,/*获取后台的名字数据*/
                }
                this.user = {...user}
                setSessionStoreage('user',user)
                setSessionStoreage('username',res.data.data.username)
                setSessionStoreage('routerpath',null)
                setSessionStoreage('routername',null)
                setSessionStoreage('userdata',res.data.data)
                setSessionStoreage('token',res.data.data.token)
                return res.data.code
            }
            // return 200
        })
    }
    @action
    loginOut=()=>{
        return new Promise((resolve, reject)=>{
            sessionStorage.removeItem('user')
            sessionStorage.removeItem('username')
            sessionStorage.removeItem('routerpath')
            sessionStorage.removeItem('routername')
            sessionStorage.removeItem('userdata')
            resolve('退出登录')
        })
    }
    @action
    register=(obj)=>{
        let {password,tel,username}=obj
        console.log(obj)
        return axios({
            url:url.useregister,
            method:'post',
            data:{password,tel,username}
        }).then(res=>{
            console.log(res)
            // return res.data.code/*看下后端返回的东西*/
            return 200/*看下后端返回的东西*/
        })
    }
}

export default LoginStore